package com.lzh.runke.core.aspect;

import lombok.extern.slf4j.Slf4j;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Before;
import org.springframework.stereotype.Component;

/**
 * Controller 日志切面
 */
@Slf4j
@Aspect
@Component
public class ControllerLogAspect {

    /**
     * 方法执行前打印参数（仅log.debug=true时记录）
     * @param joinPoint
     */
    @Before("execution(* com.lzh.runke..controller..*(..))")
    public void doBefore(JoinPoint joinPoint) {
        // 如果日志未开启debug模式则跳过
        if(!log.isDebugEnabled()) {
            return;
        }
        Object[] args = joinPoint.getArgs();
        String className = joinPoint.getTarget().getClass().getName();
        String methodName = joinPoint.getSignature().getName();

        log.debug("调用{}.{}参数:", className, methodName);
        if(args.length > 0) {
            for(int i = 0; i < args.length; i++) {
                log.debug("第{}个参数: {}", i, args[i]);
            }
        }
    }
}
